home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / PrintfGWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  3.0 KB  |  108 lines  |  [TEXT/MMCC]

  1. /*
  2. PrintfGWorld.c
  3.  
  4.     void PrintfGWorld(GWorldPtr our);
  5. Uses "printf" to print out the GWorld as a bitmap, representing blank as '.' and
  6. non-blank as '#'. The origin is designated '+'. Uses the upper lefthand pixel
  7. as the definition of blank (a poor but convenient assumption). This is very crude, 
  8. and intended only for debugging.
  9.  
  10.     void PrintStringAsBitmap(unsigned char *s);
  11. Uses DrawString to render the string in a GWorld, and then calls PrintfGWorld to
  12. render the GWorld onto the console. For debugging.
  13.  
  14. HISTORY:
  15. 1/17/94 dgp wrote it.
  16. 6/18/94 dgp Call SetGWorld(GetMainDevice())) before calling printf.
  17. 6/22/94 dgp Fixed bug: I was "restoring" to a GWorldPtr that was never initialized.
  18. 9/5/94 dgp removed assumption in printf's that int==short.
  19. */
  20. #include "VideoToolbox.h"
  21. void PrintfGWorld(GWorldPtr our);
  22. void PrintStringAsBitmap(unsigned char *s);
  23. #if __MWERKS__
  24.     #include <SIOUX.h>
  25. #endif
  26.  
  27. void PrintfGWorld(GWorldPtr world)
  28. // Print GWorld on the console as a bitmap.
  29. // Assume upper lefthand pixel is blank, which is rendered as '.'. 
  30. // Any other pixel value is represented as '#'.
  31. {
  32.     Rect r;
  33.     register unsigned long *pix,blank;
  34.     register int i,n;
  35.     int y;
  36.     char *bit;
  37.     GDHandle oldDevice;
  38.     long qD=0;
  39.   
  40.     Gestalt(gestaltQuickdrawVersion,&qD);
  41.     if(qD>=gestalt8BitQD){
  42.         oldDevice = GetGDevice();
  43.         SetGDevice(GetMainDevice());    // needed for printf
  44.     }
  45.     r=world->portRect;
  46.     r.left=0;
  47.     #if (THINK_C || THINK_CPLUS)
  48.         r.right=console_options.ncols;
  49.     #elif __MWERKS__
  50.         r.right=SIOUXSettings.columns;
  51.     #else
  52.         r.right=72;
  53.     #endif
  54.     CenterRectInRect(&r,&world->portRect);
  55.     SectRect(&r,&world->portRect,&r);
  56.     n=r.right-r.left;
  57.     bit=(char *)malloc((1+n)*sizeof(char));
  58.     if(bit==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n",(long)n+1);
  59.     bit[n]=0;
  60.     pix=(unsigned long *)malloc(n*sizeof(unsigned long));
  61.     if(pix==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n"
  62.         ,n*sizeof(long));
  63.     GetWindowPixelsQuickly((WindowPtr)world,world->portRect.left,world->portRect.top,pix,1);
  64.     blank=pix[0];
  65.     for(y=r.top;y<r.bottom;y++){
  66.         GetWindowPixelsQuickly((WindowPtr)world,r.left,y,pix,n);
  67.         for(i=0;i<n;i++){
  68.             if(pix[i]!=blank) bit[i]='#';                    // non-blank
  69.             else bit[i]='.';                                // blank
  70.         }
  71.         if(y==0 && -r.left>=0 && -r.left<n) bit[-r.left]='+';    // the origin
  72.         printf("%s\n",bit);
  73.     }
  74.     free(pix);
  75.     free(bit);
  76.     if(qD>=gestalt8BitQD)SetGDevice(oldDevice);
  77. }
  78.  
  79. void PrintStringAsBitmap(unsigned char *s)
  80. {
  81.     GWorldPtr world,old;
  82.     GDHandle oldDevice;
  83.     FontInfo f;
  84.     Rect r;
  85.     int error;
  86.  
  87.     // Draw string into a new GWorld
  88.     GetFontInfo(&f);
  89.     SetRect(&r,0,-f.ascent,StringWidth(s),f.descent);    // nominal size
  90.     InsetRect(&r,-(f.widMax/2+2),-(f.leading+2));        // add margin
  91.     error=NewGWorld(&world,1,&r,NULL,NULL,0);
  92.     if(error)PrintfExit("PrintStringAsBitmap: NewGWorld error %d.\n",error);
  93.     GetGWorld(&old,&oldDevice);
  94.     SetGWorld(world,NULL);
  95.     TextFace(old->txFace);
  96.     TextFont(old->txFont);
  97.     TextSize(old->txSize);
  98.     EraseRect(&world->portRect);
  99.     MoveTo(0,0);
  100.     DrawString(s);
  101.     SetGWorld(old,oldDevice);
  102.     
  103.     PrintfGWorld(world);
  104.     
  105.     DisposeGWorld(world);
  106. }
  107.  
  108.